package com.icss.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.icss.beans.CourseVO;
import com.icss.beans.HomeWorkVO;
import com.icss.beans.StuClassVO;

public class HomeWorkDao {
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
	private ResultSet rs=null;
	private Dao dao=new Dao();
	public void close(){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		dao.close();
	}
	
	
	public List<HomeWorkVO> getDIRName(int classID,int parentID){
		String sql = "select * from homeWork where stuClassID="+classID+" and parentID="+parentID;
		rs= dao.query(sql);
		List<HomeWorkVO> list = new ArrayList<HomeWorkVO>();
		try {
			while(rs.next()){
				HomeWorkVO vo = new HomeWorkVO();
				vo.setChildID(rs.getString("childID"));
				vo.setExID(rs.getInt("exID"));
				vo.setExName(rs.getString("exName"));
				vo.setFileSize(rs.getInt("fileSize"));
				vo.setFileURL(rs.getString("fileURL"));
				vo.setIsFile(rs.getString("isFile"));
				vo.setParentID(rs.getInt("parentID"));
				vo.setRemark(rs.getString("remark"));
				vo.setStuClassID(rs.getInt("stuClassID"));
				vo.setUid(rs.getInt("uid"));
				vo.setUploadTime(sdf.format(rs.getDate("uploadTime")));
				list.add(vo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return list;
	}

	public List<StuClassVO> getStuClass(){
		String sql ="select * from StuClass";
		rs=dao.query(sql);
		List<StuClassVO> list = new ArrayList<StuClassVO>();
		try {
			while(rs.next()){
				StuClassVO vo = new StuClassVO();
				vo.setClassName(rs.getString("className"));
				vo.setStuClassId(rs.getInt("stuClassId"));
				
				list.add(vo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return list;
	}
	
	public String getclassName(int id){
		String sql =  "select className from StuClass where stuClassID="+id;
		rs=dao.query(sql);
		String className="";
		try {
			if(rs.next()){
				className=rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return className;
	}
	
	public List<CourseVO> getCourseDIR(int id){
		String sql = "select * from Course where stuClassID="+id;
		Dao dao = new Dao();
		rs=dao.query(sql);
		List<CourseVO> list = new ArrayList<CourseVO>();
		try {
			while(rs.next()){
				CourseVO vo = new CourseVO();
				vo.setCourseID(rs.getInt("courseID"));
				vo.setCourseName(rs.getString("courseName"));
				vo.setStuClassID(rs.getInt("stuClassID"));
				vo.setPublishTime(sdf.format(rs.getDate("publishTime")));
				list.add(vo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return list;
	}
	
	public String getCourseName(int id){
		String sql = "select courseName from Course where courseID="+id;
		Dao dao = new Dao();
		rs =dao.query(sql);
		String courseName ="";
		try {
			if(rs.next()){
				courseName = rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return courseName;
	}
	
	public void addHomeWorkDIR(HomeWorkVO vo){
		String sql ="insert into homeWork (exName,uid,parentID,isFile,stuClassID) values ('"+vo.getExName()+"',"+vo.getUid()+","+vo.getParentID()+",0,"+vo.getStuClassID()+")";
		Dao dao = new Dao();
		dao.execute(sql);
		dao.close();
		close();
	}
	
	public int getStuClassID(int id){
		String sql ="select stuClassID from Course where courseID="+id;
		rs = dao.query(sql);
		int stuClassID =0;
		try {
			if(rs.next()){
				stuClassID = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return stuClassID;
	}
	
	public void addHomeWorkDIR2(int id ,String name){
		String sql ="insert into Course (courseName,stuClassID) values('"+name+"',"+id+");";
		Dao dao = new Dao();
		dao.execute(sql);
		dao.close();
		close();
		
	}
}
